<?php
class My_Nganluong{
    
    public static function cardCharge($chargeData){
        $configPrice = new Zend_Config_Ini(CONFIG_PATH.'/nganluong.ini', APPLICATION_ENV);
        $nganluong=$configPrice->nganluong->toArray();                
        $params = array('func'                  => 'CardCharge',
                        'version'               => '2.0',
                        'merchant_id'           => $nganluong['merchant_id'],
                        'merchant_account'      => $nganluong['merchant_account'],
                        'merchant_password'     => md5($nganluong['merchant_id'].'|'.$nganluong['merchant_password']),
                        'pin_card'              => $chargeData['pin_card'],
                        'card_serial'           => $chargeData['card_serial'],
                        'type_card'             => $chargeData['type_card'],    
                        'ref_code'              => $chargeData['ref_code'],
                        'client_fullname'       => $chargeData['client_fullname'],
                        'client_email'          => $chargeData['client_email'],
                        'client_mobile'         => $chargeData['client_mobile']
                        );
        $post_field = '';
        foreach ($params as $key => $value){
            if ($post_field != '')
                $post_field .= '&';
            $post_field .= $key."=".$value;
        }   
        //pr($params);
        $api_url = "https://www.nganluong.vn/mobile_card.api.post.v2.php"; 
        $ch = curl_init();                
        curl_setopt($ch, CURLOPT_URL,$api_url);                
        curl_setopt($ch, CURLOPT_ENCODING , 'UTF-8');
        curl_setopt($ch, CURLOPT_VERBOSE, 1);    
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field);
        $result = curl_exec($ch);//pr($result);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        //pr($result);                
        if ($result != '' && $status==200){                    
            $arr_result = explode("|",$result); 
            if (count($arr_result) == 13) {                 
                $arr_result['error_code']           = $arr_result[0];            
                $arr_result['merchant_id']          = $arr_result[1];
                $arr_result['merchant_account']     = $arr_result[2];
                $arr_result['pin_card']             = $arr_result[3];
                $arr_result['card_serial']          = $arr_result[4];
                $arr_result['type_card']            = $arr_result[5];
                $arr_result['ref_code']             = $arr_result[6];
                $arr_result['client_fullname']      = $arr_result[7];
                $arr_result['client_email']         = $arr_result[8];
                $arr_result['client_mobile']        = $arr_result[9];
                $arr_result['card_amount']          = $arr_result[10];
                $arr_result['transaction_amount']   = $arr_result[11];
                $arr_result['transaction_id']       = $arr_result[12];
                return $arr_result;
                //pr($arr_result);
                //if ($error_code == '00'){
//                    echo(" Nạp thẻ thành công, mệnh giá thẻ = ".$card_amount);
//                }else
//                    echo("Có lỗi khi nạp thẻ, mã lỗi ".$error_code);
            }
        }else 
            return $result;
            //echo("Có lỗi khi xử lý: ".$error);
        
              
    }   
}